/**
 * 
 */
package com.xzj.enumspecs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 以下程序练习该Card课程。它在命令行上使用两个整数参数，分别表示要发牌的手数和每手牌的张数：
 */
public class Deal {

    public static void main(String args[]) {
        int numHands = Integer.parseInt(args[0]);
        int cardsPerHand = Integer.parseInt(args[1]);
        List<Card> deck = Card.newDeck();
        Collections.shuffle(deck);
        for (int i = 0; i < numHands; i++)
            System.out.println(dealHand(deck, cardsPerHand));
    }

    /**
     * Returns a new ArrayList consisting of the last n elements of deck, which are
     * removed from deck. The returned list is sorted using the elements' natural
     * ordering.
     */
    public static <E extends Comparable<E>> ArrayList<E> dealHand(List<E> deck, int n) {
        int deckSize = deck.size();
        List<E> handView = deck.subList(deckSize - n, deckSize);
        ArrayList<E> hand = new ArrayList<E>(handView);
        handView.clear();
        Collections.sort(hand);
        return hand;
    }
}
